#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "def.h"
#include "stu.h"

int main() {
    help();
    int get_number, op;
    printf("请输入您的选择（0到19）  \n");
    scanf("%d", &get_number);
    SqList L, newL;
    L.elem = NULL;
    newL.elem = NULL;
    int i, loc, num, max;
    ElemType e, pre, next;
    char FileName[30], file[15] = "data.txt";
    LISTS Lists;
    Lists.length = 0;
    while (get_number) {
        switch (get_number) {
        case 1:
            i = InitList(L);
            switch (i) {
            case INFEASIBLE:
                printf("线性表存在\n");
                break;
            case ERROR:
                printf("失败\n");
                break;
            case OK:
                printf("成功\n");
                break;
            }
            break;
        case 2:
            if (locate(L, Lists) > 0) {
                strcpy(FileName, Lists.elem[locate(L, Lists) - 1].name);
                i = RemoveList(Lists, FileName);
                switch (i) {
                case INFEASIBLE:
                    printf("此列表为空\n");
                    break;
                case ERROR:
                    printf("不存在此线性表\n");
                    break;
                case OK:
                    L.elem = NULL;
                    printf("成功\n");
                    break;
                }
                break;
            } else {
                i = DestroyList(L);
                switch (i) {
                case INFEASIBLE:
                    printf("线性表不存在\n");
                    break;
                case ERROR:
                    printf("失败\n");
                    break;
                case OK:
                    printf("成功\n");
                    break;
                }
                break;
            }
            break;
        case 3:
            i = ClearList(L);
            switch (i) {
            case INFEASIBLE:
                printf("线性表不存在\n");
                break;
            case ERROR:
                printf("失败\n");
                break;
            case OK:
                printf("成功\n");
                break;
            }
            break;
        case 4:
            i = ListEmpty(L);
            switch (i) {
            case TRUE:
                printf("此线性表为空\n");
                break;
            case FALSE:
                printf("此线性表不为空\n");
                break;
            case INFEASIBLE:
                printf("此线性表不存在\n");
                break;
            }
            break;
        case 5:
            i = ListLength(L);
            if (i == INFEASIBLE)
                printf("线性表为空\n");
            else
                printf("%d\n", ListLength(L));
            break;
        case 6:
            printf("请输入第几个元素\n");
            scanf("%d", &num);
            i = GetElem(L, num, pre);
            switch (i) {
            case INFEASIBLE:
                printf("线性表不存在\n");
                break;
            case ERROR:
                printf("您输入的数据不合法\n");
                break;
            case OK:
                printf("第%d个元素是%d\n", num, pre);
                break;
            }

            break;
        case 7:
            printf("请输入您要查找的元素\n");
            scanf("%d", &e);
            loc = LocateElem(L, e);
            if (loc == 0)
                printf("线性表中无此元素\n");
            else if (loc == -1)
                printf("此线性表为空\n");
            else
                printf("第%d个元素是%d\n", loc, e);
            break;
        case 8:
            printf("请输入元素\n");
            scanf("%d", &e);
            i = PriorElem(L, e, pre);
            switch (i) {
            case INFEASIBLE:
                printf("此线性表为空\n");
                break;
            case ERROR:
                printf("无前驱\n");
                break;
            case OK:
                printf("前驱是%d\n", pre);
                break;
            }
            break;
        case 9:
            printf("请输入元素\n");
            scanf("%d", &e);
            i = NextElem(L, e, next);
            switch (i) {
            case INFEASIBLE:
                printf("此线性表为空\n");
                break;
            case ERROR:
                printf("无后继\n");
                break;
            case OK:
                printf("后继是%d\n", next);
                break;
            }
            break;
        case 10:
            printf("请输入插入位置及元素 \n");
            scanf("%d %d", &num, &e);
            i = ListInsert(L, num, e);
            switch (i) {
            case INFEASIBLE:
                printf("此线性表为空\n");
                break;
            case ERROR:
                printf("数据不合法\n");
                break;
            case OVERFLOW:
                printf("数据溢出 插入失败\n");
                break;
            case OK:
                printf("成功\n");
                break;
            }
            break;
        case 11:
            printf("请输入被删除元素的位置\n");
            scanf("%d", &num);
            i = ListDelete(L, num, e);
            switch (i) {
            case INFEASIBLE:
                printf("此线性表为空\n");
                break;
            case ERROR:
                printf("数据不合法\n");
                break;
            case OK:
                printf("成功，被删除元素是%d\n", e);
                break;
            }
            break;
        case 12:
            i = ListTraverse(L);
            switch (i) {
            case INFEASIBLE:
                printf("线性表不存在\n");
                break;
            case OK:
                printf("成功\n");
            }
            break;
        case 13:
            i = SaveList(L, file);
            switch (i) {
            case INFEASIBLE:
                printf("线性表不存在\n");
                break;
            case OK:
                printf("成功\n");
                break;
            }
            break;
        case 14:
            i = LoadList(L, file);
            switch (i) {
            case ERROR:
                printf("打开文件失败\n");
                break;
            case OK:
                printf("成功\n");
                ListTraverse(L);
                break;
            }
            break;
        case 15:
            filehelp();
            printf("请能输入您的选择(0-4)\n");
            scanf("%d", &op);
            while (op) {
                switch (op) {
                case 1:
                    printf("请输入新线性表的名字\n");
                    scanf("%s", FileName);
                    i = AddList(Lists, FileName);
                    switch (i) {
                    case INFEASIBLE:
                        printf("线性表不存在\n");
                        break;
                    case ERROR:
                        printf("失败\n");
                        break;
                    case OK:
                        for (int i = 0; i < Lists.length; i++) {
                            printf("%s\n", Lists.elem[i].name);
                            ListTraverse(Lists.elem[i].L);
                        }
                        printf("成功\n");
                        break;
                    }
                    break;
                case 2:
                    printf("请输入线性表的名字\n");
                    scanf("%s", FileName);
                    i = RemoveList(Lists, FileName);
                    switch (i) {
                    case INFEASIBLE:
                        printf("此列表为空\n");
                        break;
                    case ERROR:
                        printf("不存在此线性表\n");
                        break;
                    case OK:
                        for (int i = 0; i < Lists.length; i++) {
                            printf("%s\n", Lists.elem[i].name);
                            ListTraverse(Lists.elem[i].L);
                        }
                        printf("成功\n");
                        break;
                    }
                    break;
                case 3:
                    printf("请输入线性表的名字\n");
                    scanf("%s", FileName);
                    loc = LocateList(Lists, FileName);
                    if (loc == 0)
                        printf("未找到\n");
                    else if (loc == -1)
                        printf("此列表为空\n");
                    else
                        printf("线性表%s的位置序号是%d\n", FileName, loc);
                    break;
                case 4:
                    filehelp();
                    break;
                default:
                    printf("请重新输入\n");
                    break;
                }
                printf("请能输入您的选择(0-4)\n");
                scanf("%d", &op);
            }
            break;
        case 16:
            if (Lists.length == 0) {
                printf("无多线性表\n");
                break;
            }
            printf("请输入线性表名字\n");
            scanf("%s", FileName);
            if (LocateList(Lists, FileName) <= 0) {
                printf("无此线性表\n");
                break;
            }
            if (LocateList(Lists, FileName) > 0) {
                // if (L.elem != NULL)
                //     DestroyList(L);
                L = Lists.elem[LocateList(Lists, FileName) - 1].L;
            }
            break;
        case 17:
            max = MaxSubArray(L);
            printf("%d\n", max);
            break;
        case 18:
            i = sortList(L);
            switch (i) {
            case INFEASIBLE:
                printf("此线性表不存在\n");
                break;
            case OK:
                ListTraverse(L);
                printf("成功\n");
                break;
            }
            break;
        case 19:
            help();
            break;
        default:
            printf("请重新输入您的选择（0-19）！\n");
            break;
        }
        printf("请输入您的选择（0到19）\n");
        scanf("%d", &get_number);
    }
    printf("谢谢您的使用！\n");
    return 0;
}